/*
 * Copyright 2014 Cungmua, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package cungmua.vn.kiosk.activity;

import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;
import cungmua.vn.kiosk.AbstractActivity;
import cungmua.vn.kiosk.R;
import cungmua.vn.kiosk.db.DatabaseHandler;
import cungmua.vn.kiosk.model.DonHang;
import cungmua.vn.kiosk.model.NhanVien;
import cungmua.vn.kiosk.model.SKU;
import cungmua.vn.kiosk.network.CallServices;
import cungmua.vn.kiosk.tabs.BadgeTabManager;
import cungmua.vn.kiosk.tabs.TabGroupActivity;
import cungmua.vn.kiosk.utils.WriteLog;

/**
 * @author kelvin.nguyen
 * 
 */
public class DonHangKhachHuy extends AbstractActivity {
	private DonHang donhang;
	private DatabaseHandler db;
	private Button btn_xac_nhan;
	private NhanVien nhanvien;
	private int option;
	private RadioGroup radioGroupsHuy;
	private RadioButton radioButton;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_donhang_khachhuy);
		db = new DatabaseHandler(self);

		nhanvien = db.getAllNhanviens().get(0);

		initview();
		Bundle bundle = getIntent().getExtras();
		if (bundle != null) {
			donhang = bundle.getParcelable(DONHANG);
		}
	}

	public void initview() {
		radioGroupsHuy = (RadioGroup) findViewById(R.id.radioHuy);
		btn_xac_nhan = (Button) findViewById(R.id.btn_xac_nhan);
		btn_xac_nhan.setOnClickListener(onclick);
	}

	public OnClickListener onclick = new OnClickListener() {

		@Override
		public void onClick(View v) {
			switch (v.getId()) {
			
			case R.id.btn_xac_nhan:
				// update status donhang
				donhang.setStatus(DH_STATUS_CANCEL);
				donhang.setStatus_trave(DH_STATUS_CANCEL);// Trang thai don hang
															// bi huy
				db.updateDonhang(donhang);

				for (SKU sku : db.getAllSKUByOrderId(donhang.getOrderId())) {

					// update status sku of order for reused screen report
					// sku.setSku_status(0);// status=0 delivery faild
					db.updateSku(0, sku.getQuantity(), donhang.getOrderId(),
							sku.getSkuId());

					// get selected radio button from radioGroup
					int selectedId = radioGroupsHuy.getCheckedRadioButtonId();

					// find the radiobutton by returned id
					radioButton = (RadioButton) findViewById(selectedId);
					switch (radioButton.getId()) {
					case R.id.radioKhachdoiy:
						option = REASON_KHACHHUYDODOIY;
						break;
					case R.id.radioKhachpha:
						option = REASON_KHACHHUYDODATTHU;
						break;
					case R.id.radioHangkhongphuhop:
						option = REASON_KHACHHUYDOHANGKHONGPHUHOP;
						break;
					case R.id.radioHangkhongdatchatluong:
						option = REASON_KHACHHUYDOHANGKHONGDATCHATLUONG;
						break;
					case R.id.radioKhongtintuongdoitac:
						option = REASON_KHACHHUYDOKHONGTINTUONG;
						break;
					case R.id.radioThieutien:
						option = REASON_KHACHHUYDOVUONGTHANHTOAN;
						break;
					case R.id.radioGiaohangcham:
						option = REASON_KHACHHUYDOGIAOHANGCHAM;
						break;
					case R.id.radioLenctynhan:
						option = REASON_KHACHLENCONGTYNHAN;
						break;

					default:
						break;
					}
				if(isNetworkAvailable()){
					
					new backupStatusSKU(String.valueOf(nhanvien.getId()),
							String.valueOf(donhang.getOrderId()),
							String.valueOf(donhang.getStatus()), "", "",
							sku.getSkuId() + ","
									+ sku.getSku_Quantity_Delivery() + ","
									+ option).execute();

				}else{
					donhang.setIs_Synchonize(1);
					db.updateDonhang(donhang);
					
					showToast(getResources().getString(R.string.network_unable), Toast.LENGTH_LONG);
				}
				}

				TabGroupActivity tab_cancel = (TabGroupActivity) self
						.getParent();

				tab_cancel.finishFromChild(DonHangKhachHuy.this);
				int index = db.getAllDonHangDangGiao().size();
				BadgeTabManager.getInstance().setBadgeAtIndex(index, 0);
				
				break;

			default:
				break;
			}

		}
	};

	public class backupStatusSKU extends AsyncTask<Void, Void, Integer> {

		private String user_id;
		private String order_id;
		private String status;
		private String ship_date;
		private String date_part;
		private String sku;

		public backupStatusSKU(String user_id, String order_id, String status,
				String ship_date, String date_part, String sku) {
			this.user_id = user_id;
			this.order_id = order_id;
			this.status = status;
			this.ship_date = ship_date;
			this.date_part = date_part;
			this.sku = sku;
		}

		/*
		 * (non-Javadoc)
		 * 
		 * @see android.os.AsyncTask#onPreExecute()
		 */
		@Override
		protected void onPreExecute() {
			super.onPreExecute();
		}

		@Override
		protected Integer doInBackground(Void... params) {
			String json = CallServices.BackupSigleOrder(self, user_id,
					order_id, status, ship_date, date_part, sku);

		//	WriteLog.d(TAG, json);

			return null;
		}

		/*
		 * (non-Javadoc)
		 * 
		 * @see android.os.AsyncTask#onPostExecute(java.lang.Object)
		 */
		@Override
		protected void onPostExecute(Integer result) {
			// TODO Auto-generated method stub
			super.onPostExecute(result);
		}

	}
}
